package com.lc.hongtai.ui;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lc.hongtai.R;
import com.lc.hongtai.activity.BaseActivity;
import com.lc.hongtai.adapter.BorrowManagerAdapter;
import com.lc.hongtai.dialogs.DateTimePickDialog;
import com.zcx.helper.bound.BoundView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/16.
 */
public class BorrowManagerActivity extends BaseActivity implements View.OnClickListener {

    @BoundView(R.id.borrow_manager_type_ll)
    private LinearLayout borrow_manager_type_ll;
    @BoundView(R.id.borrow_manager_type_tv)
    private TextView borrow_manager_type_tv;
    @BoundView(R.id.borrow_manager_start_tv)
    private TextView borrow_manager_start_tv;
    @BoundView(R.id.borrow_manager_end_tv)
    private TextView borrow_manager_end_tv;
    @BoundView(R.id.borrow_manager_search_bt)
    private Button borrow_manager_search_bt;
    @BoundView(R.id.borrow_manager_lv)
    private ListView borrow_manager_lv;
    private List<String> lists = new ArrayList<>();
    private List<String> entityLists = new ArrayList<>();
    private PopupWindow mPop;

    private BorrowManagerAdapter adapter;
    private String show_type = "borrowing";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_borrow_manager);

        lists.add("招标中的借款");
        lists.add("流标的借款");
        lists.add("已还清的借款");
        lists.add("逾期的借款");

        for (int i = 0; i < 30; i++) {

            entityLists.add("");

        }

        borrow_manager_lv.setAdapter(adapter = new BorrowManagerAdapter(context, entityLists));

        setListener();

    }


    private void setListener() {

        borrow_manager_type_ll.setOnClickListener(this);
        borrow_manager_start_tv.setOnClickListener(this);
        borrow_manager_end_tv.setOnClickListener(this);
        borrow_manager_search_bt.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        DateTimePickDialog dateTimePickDialog = new DateTimePickDialog(this, null);

        switch (view.getId()) {

            case R.id.borrow_manager_type_ll:

                initPop();

                break;

            case R.id.borrow_manager_start_tv:

                dateTimePickDialog.dateTimePicKDialog(borrow_manager_start_tv);

                break;

            case R.id.borrow_manager_end_tv:

                dateTimePickDialog.dateTimePicKDialog(borrow_manager_end_tv);

                break;

            case R.id.borrow_manager_search_bt:
                break;

        }

    }

    public void initPop() {

        if (mPop != null && mPop.isShowing()) {

            mPop.dismiss();

        } else {

            View view = getLayoutInflater().inflate(R.layout.pop_menulist, null);

            ListView pop_lv = (ListView) view.findViewById(R.id.menulist);

            pop_lv.setAdapter(new ArrayAdapter<>(this, R.layout.spinner_list_item, lists));

            DisplayMetrics metric = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metric);

            int height = metric.heightPixels;

//            WindowManager wm = this.getWindowManager();
//            int height2 = wm.getDefaultDisplay().getHeight();
//            Log.e("---", "height" + height + "   height2 " + height2);

            int[] arr = new int[2];
            borrow_manager_type_tv.getLocationOnScreen(arr);

//            Log.e("---", "weidth" + arr[0] + "height" + arr[1] + "bottom    " + borrow_manager_type_tv.getBottom());

            mPop = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, height - arr[1] - borrow_manager_type_tv.getBottom(), true);

//            Log.e("---", "popheight" + mPop.getHeight());

            mPop.setTouchable(true);

            mPop.setTouchInterceptor(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return false;
                }
            });

            mPop.setBackgroundDrawable(new BitmapDrawable());

            mPop.showAsDropDown(borrow_manager_type_ll);

            pop_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    borrow_manager_type_tv.setText(lists.get(position));

                    switch (position) {

                        case 0:

                            BorrowManagerAdapter adapter = new BorrowManagerAdapter(context, entityLists);
                            borrow_manager_lv.setAdapter(adapter);

                            break;

                        case 1:

                            BorrowManagerAdapter adapter1 = new BorrowManagerAdapter(context, entityLists);
                            adapter1.mType = 1;
                            borrow_manager_lv.setAdapter(adapter1);
                            break;

                        case 2:

                            BorrowManagerAdapter adapter2 = new BorrowManagerAdapter(context, entityLists);
                            adapter2.mType = 2;
                            borrow_manager_lv.setAdapter(adapter2);

                            break;

                        case 3:

                            BorrowManagerAdapter adapter3 = new BorrowManagerAdapter(context, entityLists);
                            adapter3.mType = 3;
                            borrow_manager_lv.setAdapter(adapter3);

                            break;

                    }

                    mPop.dismiss();
                }
            });

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mPop.dismiss();
                }
            });

        }
    }

}
